package com.gzsxy.sso.resources.config;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.security.oauth2.provider.token.TokenStore;
import org.springframework.security.oauth2.provider.token.store.redis.RedisTokenStore;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.CorsConfigurationSource;
import org.springframework.web.cors.UrlBasedCorsConfigurationSource;

import java.util.Arrays;

/**
 * @author xiaolong
 * @version 1.0
 * @description: 令牌存储策略
 * @date 2021/11/6 14:27
 */
@Configuration
public class TokenConfig {

    private String SIGNING_KEY = "uaa123";

    /**
     * redis连接工厂
     */
    @Autowired
    RedisConnectionFactory redisConn;


    /**
     * 令牌存储模式
     */
    @Bean
    public TokenStore getRedistTokenStore(){
        return new RedisTokenStore(redisConn);
    }



    //设置密码加密模式
    @Bean
    public PasswordEncoder passwordEncoder(){
        return new BCryptPasswordEncoder();
    }



//    /**
//     * 前后端联调----解决跨域问题
//     */
//    @Bean
//    public CorsConfigurationSource corsConfigurationSource() {
//        CorsConfiguration configuration = new CorsConfiguration();
//        // 配置那些域可以跨站请求资源
//        configuration.setAllowedOrigins(Arrays.asList("http://localhost:8080"));
//        // 配置支持的方法
//        configuration.setAllowedMethods(Arrays.asList("GET","POST"));
//        configuration.applyPermitDefaultValues();
//        UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
//        source.registerCorsConfiguration("/**", configuration);
//        return source;
//    }



    public static void main(String[] args) {
        BCryptPasswordEncoder encoder = new BCryptPasswordEncoder();
        String encode = encoder.encode("client_secret");
        System.out.println(encode);
    }
}
